<template>
  <a-spin :spinning='confirmLoading'>
    <j-form-container :disabled='formDisabled'>
      <!-- 主表单区域 -->
      <a-form :form='form' slot='detail'>
        <a-row>
          <a-col :span='8'>
            <a-form-item label='商机编号' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['opportunityNum']" placeholder='选择商机名称即可自动生成' disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='8'>
            <a-form-item label='客户简称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['shortName']" placeholder='选择商机名称即可自动生成' disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='8'>
            <a-form-item label='商机名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-popup
                placeholder='请选择商机名称'
                v-decorator="['opportunityName',validatorRules.opportunityName]"
                :trigger-change='true'
                org-fields='id,name,num,short_name,sale_leader'
                dest-fields='opportunityId,opportunityName,opportunityNum,shortName,saleLeader'
                code='bus_business_opportunity_select'
                :param='{saleLeader: $store.getters.userInfo.realname}'
                @callback='popupCallback'
              />
              <a-input v-decorator="['opportunityId']" v-show='false'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='8'>
            <a-form-item label='销售经理' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['saleLeader']" placeholder='选择商机名称即可自动生成' disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='8'>
            <a-form-item label='活动类型' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-dict-select-tag type='list' v-decorator="['type',validatorRules.type]" :trigger-change='true'
                                 dictCode='icm_bus_sale_activity_type' placeholder='请选择活动类型' />
            </a-form-item>
          </a-col>

          <a-col :span='8'>
            <a-form-item label='开始日期' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-date placeholder='请选择开始日期' v-decorator="['startTime',validatorRules.startTime]" :trigger-change='true'
                      style='width: 100%' />
            </a-form-item>
          </a-col>
          <a-col :span='8'>
            <a-form-item label='结束日期' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-date placeholder='请选择结束日期' v-decorator="['endTime',validatorRules.endTime]" :trigger-change='true'
                      style='width: 100%' />
            </a-form-item>
          </a-col>
          <a-col :span='24'>
            <a-form-item label='参与人' :labelCol='labelCol2' :wrapperCol='wrapperCol2'>
              <a-textarea v-decorator="['participants']" rows='3' placeholder='请输入参与人' />
            </a-form-item>
          </a-col>
          <a-col :span='24'>
            <a-form-item label='活动内容' :labelCol='labelCol2' :wrapperCol='wrapperCol2'>
              <a-textarea v-decorator="['content',validatorRules.content]" rows='3' placeholder='请输入活动内容' />
            </a-form-item>
          </a-col>
          <a-col v-if='showFlowSubmitButton' :span='24' style='text-align: center'>
            <a-button @click='submitForm' type='primary'>提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

import pick from 'lodash.pick'
import { getAction, httpAction } from '@/api/manage'
import { FormTypes, getRefPromise } from '@/utils/JEditableTableUtil'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'BusSaleActivityForm',
  mixins: [],
  components: {},
  data() {
    return {
      form: this.$form.createForm(this),
      confirmLoading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 2 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 22 }
      },
      validatorRules: {
        type: {
          rules: [
            { required: true, message: '请输入活动类型' }
          ]
        },
        startTime: {
          rules: [
            { required: true, message: '请输入开始日期' }
          ]
        },
        endTime: {
          rules: [
            { required: true, message: '请输入结束日期' }
          ]
        },
        content: {
          rules: [
            { required: true, message: '请输入活动内容' }
          ]
        },
        opportunityName: {
          rules: [
            { required: true, message: '请输入商机名称' }
          ]
        }
      },
      url: {
        add: '/bussiness/busSaleActivity/add',
        edit: '/bussiness/busSaleActivity/edit',
        queryById: '/bussiness/busSaleActivity/queryById'
      },
      fields: [
        'type', 'content', 'startTime', 'endTime', 'participants', 'opportunityId', 'opportunityName', 'remarks', 'opportunityNum', 'shortName',
        'saleLeader','createBy'
      ]
    }
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：false流程表单 true普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    add() {
      this.edit({})
    },
    addRecord(record) {
      this.edit(record);
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    addBefore() {
      this.form.resetFields()
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter() {
      let fieldval = pick(this.model, this.fields)
      this.$nextTick(() => {
        this.form.setFieldsValue(fieldval)
      })
    },
    /** 整理成formData */
    classifyIntoFormData(allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      return {
        ...main, // 展开
        busDocumentList: allValues.tablesValue[0].values
      }
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    validateError(msg) {
      this.$message.error(msg)
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    }

  }
}
</script>

<style scoped>
</style>